import 'package:fluro/fluro.dart';
import 'package:fluro/src/router.dart';
import 'package:flutter/material.dart';
import 'package:flutter_shop/cons/global_cons.dart';
import 'package:flutter_shop/pages/common/common_input_page/common_input_page.dart';
import 'package:flutter_shop/pages/common/common_input_page/entity/common_input_data.dart';
import 'package:flutter_shop/pages/common/login/login_page.dart';
import 'package:flutter_shop/pages/common/splash_page/splash_page.dart';
import 'package:flutter_shop/pages/common/web_view_page/web_view_page.dart';
import 'package:flutter_shop/routers/router_init.dart';
import 'package:flutter_shop/utils/utils.dart';

import 'index_page/index_page.dart';

///常规路由
class CommonRouter implements IRouterProvider {
  ///闪屏
  static String splashPage = '/splashPage';

  ///入口页
  static String indexPage = '/indexPage';

  ///登录页
  static String loginPage = '/loginPage';

  ///web_view
  static String webViewPage = '/webViewPage';

  ///常规输入页
  static String inputPage = '/inputPage';

  @override
  void initRouter(Router router) {
    //闪屏
    router.define(splashPage, handler: Handler(handlerFunc: (_, params) => SplashPage()));

    //入口页
    router.define(indexPage, handler: Handler(handlerFunc: (_, params) => IndexPage()));

    //登录页
    router.define(loginPage, handler: Handler(handlerFunc: (_, params) {
      return LoginPage();
    }));

    //webview
    router.define(webViewPage, handler: Handler(handlerFunc: (_, params) {
      String title = params[GlobalCons.KEY_TITLE]?.first;
      String url = params[GlobalCons.KEY_URL]?.first;
      return WebViewPage(title: title, url: url);
    }));

    //常规输入页
    router.define(inputPage, handler: Handler(handlerFunc: (_, params) {
      String json = params[GlobalCons.KEY_DATA].first;
      CommonInputData item = Utils.parseJson2Obj(json, (v) => CommonInputData().fromJson(v));

      return CommonInputPage(
        title: item.title,
        content: item.content,
        hintText: item.hintText,
        maxLength: item.maxLength,
        inputType: item.inputType,
      );
    }));
  }
}
